package com.learngittesting.dsnTest.Controller;

/**
 * 序列化
 * @author DongSinan
 * @date 2022/2/17 17:22
 */
import com.learngittesting.dsnTest.vo.Student;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * Redis控制
 * @author DongSinan
 * @date 2022/2/17 17:14
 */
@RestController
public class RedisController {


    //添加数据到Redis
    @Resource
    private RedisTemplate redisTemplate;

    @Resource
    private StringRedisTemplate stringRedisTemplate;


    @PostMapping("/redis/addstring")
    public String addToRddis(String name, String value) {

        //操作Redis的String
        ValueOperations valueOperations=redisTemplate.opsForValue();

        //添加数据到Redis
        valueOperations.set("myname","lisi");

        return "向Redis添加数据";
    }

    @PostMapping("redis/{k}/{v}")
    public String addToKV(@PathVariable String k,
                          @PathVariable String v)
    {
        stringRedisTemplate.opsForValue().set(k, v);
        return "使用stringRedisTemplate对象";
    }

    @GetMapping("redis/getKV/{k}")
    public String getToKV(@PathVariable String k)
    {
        String v=stringRedisTemplate.opsForValue().get(k);

        return "使用stringRedisTemplate对象,"+k+"值为"+v;
    }


    @GetMapping ("/redis/getK")
    public String getData(String k)
    {
        //操作Redis的String
        ValueOperations valueOperations=redisTemplate.opsForValue();

        Object v=valueOperations.get(k);

        return "k="+k+" 值为="+v;
    }

    @PostMapping("/redis/addstr")
    public String addString(String k,String v)
    {
        //使用RedisTemplate
        //设置key使用String序列化
        redisTemplate.setKeySerializer(new StringRedisSerializer());

        //设置value的序列化
        redisTemplate.setValueSerializer(new StringRedisSerializer());

        //设置传入的k,v使用此序列化
        redisTemplate.opsForValue().set(k,v);

        return "定义RedisTemplate序列化";
    }

    @PostMapping("/redis/addjson/{ID}/{Age}/{Name}")
    public String addJson(@PathVariable Integer ID,@PathVariable Integer Age,@PathVariable String Name)
    {
        Student student=new Student();
        student.setName(Name);
        student.setAge(Age);
        student.setId(ID);

        redisTemplate.setKeySerializer(new StringRedisSerializer());

        //把Value转为Json序列化
        redisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer(Student.class));

        redisTemplate.opsForValue().set("mystudents",student);

        return "使用json序列化";
    }


    @GetMapping("/redis/getjson")
    public String addJson()
    {

        redisTemplate.setKeySerializer(new StringRedisSerializer());

        //把Value转为Json序列化
        redisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer(Student.class));

        Object ValueMtudents = redisTemplate.opsForValue().get("mystudents");

        return "使用json序列化,mystudents的值为："+ValueMtudents;
    }
}